﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;


namespace YouHP.Pages.Ajax
{
    public class ActionSendAjax : YouHP.Pages.BasePage
    {

        protected int toUserId;
        protected string receivers;
        protected StringBuilder sb = new StringBuilder();

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            //向单个用户发互动
            toUserId = RequestTools.GetInt("toUserId", 0);
            //向批量用户发互动
            receivers = RequestTools.GetQueryString("receivers");
    
            if (toUserId > 0)
            {
                ActionInfo info = ValidataForm();
                if (info == null) return;

                bool succ = ActionBLL.Instance.AddAction(info);//执行添加
                if (succ)
                {
                    sb.Append("发送成功！");

                }
                else
                {
                    sb.Append("操作失败！");

                }
                ResponseXML(sb);
                return;
            }

            if (receivers != "")
            {
                string[] array = receivers.Split(';');
                bool succ = true;
                int i;
                for (i = 0; i < array.Length; i++)
                {

                    UserInfo info = UserBLL.Instance.GetUserInfo(array[i]);
                    if (info.Id <= 0)
                    {
                        sb.Append("用户" + array[i] + "不存在！请确认您的输入无误");
                        ResponseXML(sb);
                        return;
                    }
                    ActionInfo action = new ActionInfo();
                    action.ActionType = RequestTools.GetQueryInt("actiontype", 0);
                    action.Remark = RequestTools.GetQueryString("msg").Trim();

                    action.ToUserId = info.Id;
                    action.ToUserName = info.UserName;
                    action.FromUserId = UserData.Id;
                    action.FromUserName = UserData.UserName;
                    action.FromUserAvatar = UserData.Avatar;
                    action.PostTime = DateTime.Now;

                    succ = ActionBLL.Instance.AddAction(action);
                    if (!succ) break;


                }
                if (succ)
                {
                    sb.Append("发送成功！");
                }
                else
                {
                    sb.Append("给用户" + array[i] + "发送互动失败！");
                  
                }
                ResponseXML(sb);
                return;
            }

        }

        private ActionInfo ValidataForm()
        {

            ActionInfo info = new ActionInfo();
            info.ToUserId = toUserId;
            info.Remark = RequestTools.GetString("msg").Trim();
            if (info.Remark == "")
            {
                info.Remark = "无陈述";
            }
            info.ToUserName = UserBLL.Instance.GetUserInfo(toUserId).UserName;
            info.ActionType = RequestTools.GetQueryInt("actiontype", 0);

            info.PostTime = DateTime.Now;
            info.FromUserId = UserData.Id;
            info.FromUserName = UserData.UserName;

            return info;


        }
        protected void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

    }
}
